package cn.sdormitory.controller;

import cn.sdormitory.canteen.service.FoodOrderService;
import cn.sdormitory.canteen.service.FoodSupplierGoodsService;
import cn.sdormitory.canteen.service.FoodSupplierService;
import cn.sdormitory.canteen.vo.*;
import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.common.dto.ResultMsg;
import cn.sdormitory.common.utils.ImageToTextAIUtils;
import cn.sdormitory.common.utils.RedisUtils;
import cn.sdormitory.school.entity.SchoolPatrol;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 食品安全
 */
@RestController
@RequestMapping("/app/canteen")
@Api("APP端考勤管理接口")
@Slf4j
public class AppCanteenSafeController {

    @Autowired
    private FoodSupplierService foodSupplierService;

    @Autowired
    private FoodSupplierGoodsService foodSupplierGoodsService;
    @Autowired
    private FoodOrderService foodOrderService;
    @Autowired
    private RedisUtils redisUtils;

    @Value("${file.upload.path}")
    private String file_path;

    @ApiOperation("getFoodSupplierList => 分页查询食品供应商")
    @PostMapping(value = "/getFoodSupplierList")
    public CommonResult<CommonPage<FoodSupplierVO>> getFoodSupplierList(@RequestBody Map<String,Object> params){
        params.put("supplierStatus","1");
        IPage<FoodSupplierVO> pageList = foodSupplierService.getPage(params);
        return CommonResult.success(CommonPage.restPage(pageList));
    }

    @ApiOperation("getFoodSupplierGoodsList => 分页查询食品供应商商品")
    @PostMapping(value = "/getFoodSupplierGoodsList")
    public CommonResult<CommonPage<FoodSupplierGoodsVO>> getFoodSupplierGoodsList(@RequestBody Map<String,Object> params){
        params.put("goodsStatus","1");
        IPage<FoodSupplierGoodsVO> pageList = foodSupplierGoodsService.getPage(params);
        return CommonResult.success(CommonPage.restPage(pageList));
    }

    @ApiOperation("saveFoodOrderInfo => 提交商品订单")
    @PostMapping(value = "/saveFoodOrderInfo")
    public CommonResult<String> saveFoodOrderInfo(@RequestBody FoodOrderVO vo){
        String num = foodOrderService.saveFoodOrderInfo(vo);
        if("-9".equals(num)) return CommonResult.failed(num);
        if(num!=null) {
            return CommonResult.success(num);
        }
        return CommonResult.failed();
    }

    @ApiOperation("getOrderList => 获取供应商订单列表")
    @PostMapping(value = "/getOrderList")
    public CommonResult<List<FoodOrderVO>> getOrderList(@RequestBody FoodOrderVO vo){
        List<FoodOrderVO> list = foodOrderService.getOrderList(vo);
        return CommonResult.success(list);
    }

    @ApiOperation("getOrderGoodsList => 获取供应商订单商品列表")
    @GetMapping(value = "/getOrderGoodsList")
    public CommonResult<List<FoodOrderGoodsVO>> getOrderGoodsList(@RequestParam String orderNo){
        if(StringUtils.isEmpty(orderNo)){
            return CommonResult.failed("缺少必要参数");
        }
        List<FoodOrderGoodsVO> list = foodOrderService.getOrderGoodsList(orderNo);
        return CommonResult.success(list);
    }

    @ApiOperation("getGoodsCheckList => 根据订单号获取商品验收列表列表")
    @GetMapping(value = "/getGoodsCheckListByOrderNo")
    public CommonResult<List<FoodGoodsCheckVO>> getGoodsCheckListByOrderNo(@RequestParam String orderNo){
        if(StringUtils.isEmpty(orderNo)){
            return CommonResult.failed("缺少必要参数");
        }
        List<FoodGoodsCheckVO> list = foodOrderService.getGoodsCheckListByOrderNo(orderNo);
        return CommonResult.success(list);
    }


    @ApiOperation("getGoodsCheckList => 解析图片获取对应内容")
    @GetMapping(value = "/getImgInfo")
    public CommonResult<List<String>> getImgInfo(@RequestParam String imgPath,@RequestParam String infoType){
        String token = redisUtils.get("bd_image_auth_token");
        if(StringUtils.isEmpty(token)){
            token = ImageToTextAIUtils.getAuth();
            redisUtils.set("bd_image_auth_token",token,1800);
        }
        imgPath = file_path+imgPath;
        String resultStr = ImageToTextAIUtils.getImageDoc(token,imgPath);
        log.info("getImgInfo:{}",resultStr);
        JSONObject json = JSON.parseObject(resultStr);
        List<Map<String,String>> words = (List<Map<String, String>>)json.get("words_result");
        List<String> list = ImageToTextAIUtils.getRecognitionWordsList(words,infoType);
        if(list==null || list.size()==0){
            return CommonResult.failed("识别失败，请重新上传认证图片");
        }
        return CommonResult.success(list);
    }


    @ApiOperation("saveGoodsCheckInfo => 添加商品验收记录")
    @PostMapping(value = "/saveGoodsCheckInfo")
    public CommonResult<List<String>> saveGoodsCheckInfo(@RequestBody FoodGoodsCheckVO vo){
        int num = foodOrderService.saveGoodsCheckInfo(vo);
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("getCheckGoodsByOrderNo => 查询收货订单商品列表，包括图片和签名")
    @PostMapping(value = "/getCheckGoodsByOrderNo")
    public CommonResult<List<String>> getCheckGoodsByOrderNo(@RequestBody FoodGoodsCheckVO vo){
        int num = foodOrderService.saveGoodsCheckInfo(vo);
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("confirmOrderReceiv => 收货单确认提交")
    @PostMapping(value = "/confirmOrderReceiv")
    public CommonResult<List<String>> confirmOrderReceiv(@RequestBody FoodOrderVO vo){
        int num = foodOrderService.confirmOrderReceiv(vo);
        if(num>0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("getSupplierInfoById => 根据供应商ID获取供应商信息")
    @GetMapping(value = "/getSupplierInfoById")
    public CommonResult<FoodSupplierVO> getSupplierInfoById(@RequestParam Long supplierId){
        FoodSupplierVO vo = foodSupplierService.getSupplierInfoById(supplierId);
        return CommonResult.success(vo);
    }

    @ApiOperation("getSupplierInfoList => 首页查询供应商信息")
    @GetMapping(value = "/getSupplierInfoList")
    public CommonResult<List<FoodSupplierVO>> getSupplierInfoList(){
        List<FoodSupplierVO> supplierList = foodSupplierService.getSupplierInfoList();
        return CommonResult.success(supplierList);
    }




}
